import { isObject } from '@/utils/is.js'
import { useBoolean } from './useBoolean.js'

export function useFormSearch(form, getList) {
  const formState = reactive({ ...form })
  const [searchLoading, toggleSearchLoading] = useBoolean(false)
  const [resetLoading, toggleResetLoading] = useBoolean(false)
  const handleSearch = async params => {
    toggleSearchLoading()
    const _params = isObject(params) ? { ...toRaw(formState), ...params } : toRaw(formState)
    await getList(_params)
    toggleSearchLoading()
  }
  const handleReset = async params => {
    toggleResetLoading()
    Object.assign(formState, form)
    await getList(isObject(params) ? params : {})
    toggleResetLoading()
  }
  return {
    formState,
    searchLoading,
    resetLoading,
    handleSearch,
    handleReset,
  }
}
